package com.SmartCare.service.impl.homeInfo;

import com.SmartCare.pojo.homeInfo.CareHomeDetails;
import com.SmartCare.pojo.homeInfo.CareHomeContacts;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.SmartCare.common.Result;
import com.SmartCare.service.homeInfo.ContactsService;
import com.SmartCare.service.homeInfo.DetailsService;
import com.SmartCare.common.vo.homeInfo.ContactVo;
import com.SmartCare.mapper.homeInfo.CareHomeDetailsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 养老院信息详情表 服务实现类
 */
@Service
public class DetailsServiceImpl extends ServiceImpl<CareHomeDetailsMapper, CareHomeDetails> implements DetailsService {
    @Autowired
    private CareHomeDetailsMapper careHomeDetailsMapper;
    @Resource
    private ContactsService contactsService;

    @Override
    public Result search(Integer pageNum, Integer pageSize, String keyword) {
        IPage<CareHomeDetails> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<CareHomeDetails> qw = new QueryWrapper<>();
        qw.like("name", keyword);
        return Result.success(page(iPage, qw));
    }

    @Override
    public Result add(CareHomeDetails careHomeDetails) {
        //通过雪花算法工具生成id
        long id = IdWorker.getId(careHomeDetails);
        careHomeDetails.setId(id);
        save(careHomeDetails);
        return Result.success(Long.toString(id));
    }

    @Override
    public Result edit(CareHomeDetails careHomeDetails) {
        updateById(careHomeDetails);
        return Result.success();
    }

    /**
     * 院方在线联系信息获取
     *
     * @param id
     * @return
     */
    @Override
    public Result getInfo(String careHomeName) {
        LambdaQueryWrapper<CareHomeContacts> wrapper = new LambdaQueryWrapper<>();
        CareHomeDetails one = careHomeDetailsMapper.selectOne(new LambdaQueryWrapper<CareHomeDetails>().eq(CareHomeDetails::getName, careHomeName));
        wrapper.eq(CareHomeContacts::getCareHomeId, one.getId());
        CareHomeContacts contact = contactsService.getOne(wrapper);
        ContactVo vo = new ContactVo();
        BeanUtils.copyProperties(contact, vo, "careHomeName");
        //vo.setCareHomeName(getById(contact.getCareHomeId()).getName());
        vo.setCareHomeName(careHomeName);
        return Result.success(vo);
    }

    @Override
    public Result getDetailsByName(String name) {
        CareHomeDetails one = getOne(new QueryWrapper<CareHomeDetails>().eq("name", name));
        if (one == null) {
            return Result.error("养老院名称不存在");
        }
        return Result.success(one);
    }

}
